<template>
  <el-dialog class="app-container" :title="title"  width="800px"  :visible.sync="visible" top="5vh" append-to-body >
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="5"  :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container" >
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>

      <el-col :span="19" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="姓名" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" ref="tb" @current-change="currentChange" :data="staffList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="姓名" align="center" prop="name" />
          <el-table-column label="性别" align="center" prop="gender" />
          <el-table-column label="学历" align="center" prop="education" />
          <el-table-column label="专业" align="center" prop="speciality" />
          <el-table-column label="职称" align="center" prop="title" />
          <el-table-column label="身份证号码" align="center" prop="idCardNumber" />
          <el-table-column label="联系电话" align="center" prop="telephone" />
          <el-table-column label="联系地址" align="center" prop="address" />
          <el-table-column label="入职时间" align="center" prop="entryTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.entryTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="入职部门" align="center" prop="deptName" />
          <el-table-column label="入职职务" align="center" prop="post" />
          <el-table-column label="试用薪资(元)" align="center" prop="probationSalary" />
          <el-table-column label="转正薪资(元)" align="center" prop="fullMemberSalay" />
          <el-table-column label="开户行" align="center" prop="bank" />
          <el-table-column label="银行卡号" align="center" prop="bankCardNo" />
          <el-table-column label="邮箱" align="center" prop="email" />
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <div  slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleImport" >确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>


  </el-dialog>
</template>

<script>
import { listStaff, getStaff, delStaff, addStaff, updateStaff } from "@/api/module/staff";
import { getDicts } from '@/api/system/dict/data';
import { uploadImageFile } from '@/api/tool/gen';
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {deptTreeSelect} from "@/api/system/user";

export default {
  name: "Staff",
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工表格数据
      staffList: [],
      // 弹出层标题
      title: "选择员工",
      // 是否显示弹出层
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      visible:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        gender: null,
        birthday: null,
        nativePlace: null,
        politicalOutlook: null,
        marriage: null,
        education: null,
        speciality: null,
        title: null,
        idCardNumber: null,
        graduationSchool: null,
        graduationTime: null,
        telephone: null,
        address: null,
        contactName: null,
        relationship: null,
        contactTelephone: null,
        personalPhotos: null,
        educationalPhotos: null,
        otherCertificatePhotos: null,
        skill: null,
        entryTime: null,
        post: null,
        probationSalary: null,
        fullMemberSalay: null,
        leavingCertificatePhotos: null,
        email: null,
        status: 1
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 学历
      educationOptions: [],
      // 政治面貌
      politicalOutlookOptions: [],
      dialogVisible: false,
      userId: null,
      userName: null,
      card: null,
      tel: null,
      bank: null,
      bankCardNo: null,
      deptId: null,
      deptName: null,
      basePay: null,
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    // 显示弹框
    show() {
      this.getList();
      this.visible = true;
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.dept = data.id;
      this.handleQuery();
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },


    /** 查询员工列表 */
    getList() {
      this.loading = true;
      listStaff(this.queryParams).then(response => {
        this.staffList = response.rows;
        // 产品类型判断
        let arr = this.staffList;
        arr.forEach(item => {
          item.education = this.getValue(this.educationOptions, item.education)
          item.politicalOutlook = this.getValue(this.politicalOutlookOptions, item.politicalOutlook)
        });
        this.staffList = arr;
        this.total = response.total;
        this.loading = false;
      });
    },
    getValue(arr, item) {
      for (let i = 0; i < arr.length; i++) {
        console.log(i);
        if (item === arr[i].dictValue) {
          return arr[i].dictLabel
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        gender: null,
        birthday: null,
        nativePlace: null,
        politicalOutlook: null,
        marriage: null,
        education: null,
        speciality: null,
        title: null,
        idCardNumber: null,
        graduationSchool: null,
        graduationTime: null,
        telephone: null,
        address: null,
        contactName: null,
        relationship: null,
        contactTelephone: null,
        personalPhotos: null,
        educationalPhotos: null,
        otherCertificatePhotos: null,
        skill: null,
        entryTime: null,
        post: null,
        probationSalary: null,
        fullMemberSalay: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        leavingCertificatePhotos: null,
        email: null,
        status: null,
        bank: null,
        bankCardNo: null,
        deptName2: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.userId=selection[0].id;
      this.userName=selection[0].name;
      this.card=selection[0].idCardNumber;
      this.tel=selection[0].telephone;
      this.bank=selection[0].bank;
      this.bankCardNo=selection[0].bankCardNo;
      this.deptId=selection[0].dept;
      this.deptName2=selection[0].deptName;
      this.basePay=selection[0].probationSalary;
      if (selection.length > 1) {
        this.$refs.tb.clearSelection();
        this.$refs.tb.toggleRowSelection(selection.pop());
      }
    },
    handleImport() {
      const userId = this.userId;
      if (userId == "") {
        this.$modal.msgError("请选择员工");
        return;
      }
      this.visible = false;
      const data = {
        userId:this.userId,
        userName:this.userName,
        card:this.card,
        tel:this.tel,
        bank:this.bank,
        bankCardNo:this.bankCardNo,
        deptId:this.deptId,
        deptName:this.deptName2,
        basePay:this.basePay,
      }
      this.$emit("showStaff",data);
    },
    currentChange(currentRow) {
      this.$refs.tb.toggleRowSelection(currentRow)
    },
  }
};
</script>
